You can use schema and data types to store and to manipulate
character strings. These types have the following order of
complexity:
1. CHAR type
2. PACKED ARRAY OF CHAR user-defined types
3. VARYING OF CHAR user-defined types
4. STRING predefined schema
Objects of the CHAR data type are character strings with a
length of 1 and are lowest in the order of character string
complexity. You can assign CHAR data to variables of the other
string types.
The PACKED ARRAY OF CHAR types allow you to specify fixed-length
character strings. The VARYING OF CHAR types are a VSI Pascal
extension that allows you to specify varying-length character
strings with a constant maximum length. The STRING types
provide a standard way for you to specify storage for
varying-length character strings with a maximum length that can
be specified at run time.
To provide values for variables of these types, you should use a
character-string constant (or an expression that evaluates to a
character string) instead of an array constructor. Using array
constructors with STRING and VARYING OF CHAR types generates an
error; to use array constructors with PACKED ARRAY OF CHAR
types, you must specify component values for every element in
the array (otherwise, you generate an error).
Example:
VAR
String1 : VARYING[10] OF CHAR VALUE 'abc';
1 – String
The STRING predefined schema provides a way of declaring
variable-length character strings. The compiler stores STRING
data as though it were stored in the following schema
definition:
TYPE
STRING ( Capacity : INTEGER ) = VARYING[Capacity] OF CHAR;
The syntax of the discriminated schema is as follows:
STRING ( Capacity )
The 'Capacity' is an integer in the range 1..65,535 that
indicates the length of the longest possible string.
To use the predefined STRING schema, you provide an upper bound
as the actual discriminant. Consider the following example:
VAR
Short_String : STRING( 5 ); {Maximum length of 5 characters}
Long_String : STRING( 100 ); {Maximum length of 100 characters}
You can assign string constants to STRING variables from length
0 to the specified upper bound. The compiler allocates enough
storage space to hold a string of the maximum length. A STRING
variable with length 0 is the empty string (''). You can only
use character-string constants (or expressions that evaluate to
character strings) to assign values to variables of these types;
you cannot use standard array constructors.
You can access the CAPACITY predeclared identifier as you would
a schema discriminant, and you can access the LENGTH and BODY
predeclared identifiers as you would access fields of a record.
The CAPACITY identifier allows you to access the actual
discriminant of the STRING schema; the LENGTH identifier allows
you to access the current length of the string object; and the
BODY identifier contains the current string object, including
whatever is in memory up to the capacity of the discriminated
schema.
2 – PACKED
User-defined packed arrays of characters with specific lower and upper bounds provide a method of specifying fixed-length character strings. The string's lower bound must equal 1. The upper bound establishes the fixed length of the string.
2.1 – Examples
The following example shows a declaration of a character string
variable of twenty characters:
VAR
My_String : PACKED ARRAY[1..20] OF CHAR;
Note that if the upper bound of the array exceeds 65,535, if the
PACKED reserved word is not used, or if the array's components
are not byte-sized characters, the compiler does not treat the
array as a character string.
3 – Varying of char
The VARYING OF CHAR user-defined types are a VSI Pascal
extension that provides a way of declaring variable-length
character strings with a compile-time maximum length. If you
require portable code, use the STRING predefined schema types to
specify variable-length character strings.
Syntax:
VARYING [upper-bound] OF [[attribute-list]] CHAR
The 'upper-bound' is an integer in the range from 1 through
65,535 that indicates the length of the longest possible string.
The 'attribute-list' is one or more optional identifiers that
provide additional information about the VARYING OF CHAR string
components.
To assign values to fixed-length character strings, you can use
a character-string constant (or an expression that evaluates to
a character string). When assigning into fixed-length strings,
the compiler adds blanks to extend a string shorter than the
maximum characters declared. If you specify a string longer
than the maximum characters declared, an error occurs. You can
also use an array constructor as long as you specify characters
for every component of the array as specified in the
declaration.
Although a VARYING OF CHAR is a distinct type, it possesses some
of the properties of both record and array types. A VARYING
string is actually stored as though it were a record with two
fields, LENGTH and BODY (which are predeclared identifiers in
VSI Pascal). The LENGTH field contains the length of the
current character string; the BODY field contains the string.
Either field can be accessed in the same way record fields are
accessed (VARY.LENGTH, VARY.BODY).
Example: VARYING [25] OF CHAR
This VARYING OF CHAR type could have the following values:
'Wolfgang Amadeus Mozart'
'Bach'